package com.example.goodsconsumer.controller;

import com.example.goodsconsumer.feign.GoodsFeign;
import com.example.goodsconsumer.util.GetUidUtil;
import com.myshop.b2b.commonmodule.Cart;
import com.myshop.b2b.commonmodule.Goods;
import com.myshop.b2b.commonmodule.Userorder;
import com.myshop.b2b.commonmodule.vo.Result;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author likaiping
 * @createTime 2024/7/3 16:21
 */
@RestController
@RequestMapping("/goods")
@AllArgsConstructor
public class GoodsController {

    private GoodsFeign goodsFeign;
    private RedisTemplate redisTemplate;

    //退出登录并跳转回登录页面
    @GetMapping( "quit")
    public Result quit(HttpSession session, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        System.out.println("退出登录拿到的token："+token);
        if (redisTemplate.opsForValue().get(token) != null) {
            redisTemplate.delete(token);
            System.out.println("退出登录，删除Redis中的token");
            return Result.success("退出登录成功");
        }
        return Result.error("已经退出登录，无需再次退出");

    }

    //首页查询全部商品并分类显示
    @GetMapping("/getAll")
    public Result<Map<String,List<Goods>>> getAll(){
        List<Goods> goods1 = new ArrayList<>();
        List<Goods> goods2 = new ArrayList<>();
        List<Goods> goods3 = new ArrayList<>();
        List<Goods> goods4 = new ArrayList<>();
        Map<String,List<Goods>> map = new HashMap<>();
        List<Goods> oldgoods = goodsFeign.getAllgoods();
        for (Goods newgoods : oldgoods) {
            System.out.print(newgoods.getTypes());
            if (newgoods.getTypes().equals(1)) {
                goods1.add(newgoods);
            }
            if (newgoods.getTypes().equals(2)) {
                goods2.add(newgoods);
            }
            if (newgoods.getTypes().equals(0)) {
                goods3.add(newgoods);
            }
            if (newgoods.getTypes().equals(3)) {
                goods4.add(newgoods);
            }
        }
        map.put("goods1", goods1);
        map.put("goods2", goods2);
        map.put("goods3", goods3);
        map.put("goods4", goods4);
        return Result.success(map);
    }

    //商品详情-即根据ID查询单个商品
    @GetMapping(value = "/detail")
    public Result<Goods> detail(int gid) {
        Goods goods = goodsFeign.getOnegid(gid);
        return Result.success(goods);
    }

    //商品首页搜索-根据名称模糊查找商品
    @GetMapping("/getOne")
    public Result<Map<String, List<Goods>>> getOne(String name) {
        List<Goods> goods1 = new ArrayList<>();
        List<Goods> goods2 = new ArrayList<>();
        List<Goods> goods3 = new ArrayList<>();
        List<Goods> goods4 = new ArrayList<>();
        Map<String,List<Goods>> map = new HashMap<>();
        List<Goods> oldgoods = goodsFeign.getOnegoods(name);
        for (Goods newgoods : oldgoods) {
            System.out.print(newgoods.getTypes());
            if (newgoods.getTypes().equals(1)) {
                goods1.add(newgoods);
            }
            if (newgoods.getTypes().equals(2)) {
                goods2.add(newgoods);
            }
            if (newgoods.getTypes().equals(0)) {
                goods3.add(newgoods);
            }
            if (newgoods.getTypes().equals(3)) {
                goods4.add(newgoods);
            }
        }
        map.put("goods1", goods1);
        map.put("goods2", goods2);
        map.put("goods3", goods3);
        map.put("goods4", goods4);
        return Result.success(map);
    }

    //加入购物车-将商品加入购物车并查询出全部商品如果有商品修改商品数量
    @GetMapping("/addCart")
    public Result cart(HttpServletRequest request,
                       @RequestParam("goodsname") String goodsname,
                       @RequestParam("price") String price,
                       @RequestParam("goodsId") String goodsId,
                       @RequestParam("number") String number){
        String token = request.getHeader("Authorization");
        Integer uid = GetUidUtil.getUaccount(token);
        List<Cart> usercart= goodsFeign.getAllcart(uid);
        int n = Integer.parseInt(price);
        for (int i=0;i<usercart.size();i++){
            if (usercart.get(i).getGoodid()==Integer.parseInt(goodsId)){
                goodsFeign.updateCart(Integer.parseInt(number),usercart.get(i).getId());
                return  Result.success("加入购物车成功",null);
            }
        }
        goodsFeign.intcart(goodsname, Integer.parseInt(number),n,Integer.parseInt(goodsId),uid);
        return  Result.success("加入购物车成功",null);
    }
    // 查询购物车全部商品
    @GetMapping("getAllCart")
    public  Result<List<Cart>> cartAll(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Integer uid = GetUidUtil.getUaccount(token);
        List<Cart> allCart = goodsFeign.getAllcart(uid);
        return Result.success(allCart);
    }


    //删除购物车，根据id删除购物车商品
    @GetMapping("/deleteCart")
    public Result deleteCart(@RequestParam("cartId") Integer cartId){
        if (goodsFeign.deleteCart(cartId) > 0) {
            return Result.success("删除购物车成功",null);
        }
        return Result.error("删除购物车失败");
    }
    //添加订单并调用删除购物车（即购买之后删除购物车商品）
    @GetMapping("/payGoods")
    public Result payGoods(HttpServletRequest request,String[] goodid,String[] number,String[] cartId){
        String token = request.getHeader("Authorization");
        Integer uid = GetUidUtil.getUaccount(token);
        if (goodid.length == 0||number.length== 0||cartId.length == 0){
            return  Result.error("购物车无商品");
        }else {
            for (int i = 0; i < goodid.length; i++) {
                Integer price = goodsFeign.getOnegid(Integer.parseInt(goodid[i])).getGprice();
                String goodsname = goodsFeign.getOnegid(Integer.parseInt(goodid[i])).getGname();

                goodsFeign.insertOrder(goodsname, Integer.parseInt(number[i]), Integer.parseInt(number[i]) * price, uid);
                goodsFeign.deleteCart(Integer.parseInt(cartId[i]));
            }
            return Result.success("购买成功",null);
        }
    }

    //根据用户查询登陆者的历史购买记录
    @GetMapping("/getAllOrder")
    public Result<List<Userorder>> getAllOrder(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Integer uid = GetUidUtil.getUaccount(token);
        List<Userorder> allOrder = goodsFeign.getAllorder(uid);
        return Result.success(allOrder);
    }

    @GetMapping("/editCartNum")
    public Result updateCartNumber(@RequestParam("number")int number,@RequestParam("cartId")int cartId){
        if (goodsFeign.updateCartNumber(number,cartId)) {
            return Result.success("修改购物车数量成功",null);
        }
        return Result.error("修改失败");
    }
}
